package com.asman.vr

import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModel
import com.alibaba.sdk.android.oss.ClientException
import com.alibaba.sdk.android.oss.ServiceException
import com.alibaba.sdk.android.oss.model.PutObjectRequest
import com.alibaba.sdk.android.oss.model.PutObjectResult
import com.asman.oss.my.OSSUpload
import com.asman.vr.dto.AccountRepository
import com.asman.vr.dto.BaseDataRepository
import com.asman.vr.dto.OSSConfigData
import com.asman.vr.network.CallAnswerRepository
import com.asman.vr.network.OSSType
import com.asman.vr.network.Status
import com.asman.vr.utils.OSSFullUrlUtils
import log
import showToast

class BaseWebViewModel(
        private val lifecycleOwner: LifecycleOwner,
        private val repository: AccountRepository,
        private val baseDataRepository: BaseDataRepository,
        private val callAnswerRepository: CallAnswerRepository
) : ViewModel() {

    var publicOSSConfigData: OSSConfigData? = null//公有oss配置

    var privateOSSConfigData: OSSConfigData? = null//私有oss配置

    fun getUserInfo() = repository.getUserInfo()

    fun getOSSConfigData(ossType: OSSType) = baseDataRepository.getOSSConfigData(ossType)

    init {
        getOSSConfigData()
    }

    fun getOSSConfigData() {
        getOSSConfigData(OSSType.PUBLIC).observe(lifecycleOwner, Observer {
            if (it.status == Status.SUCCESS) {
                publicOSSConfigData = it.data
            }
        })
        getOSSConfigData(OSSType.PRIVATE).observe(lifecycleOwner, Observer {
            if (it.status == Status.SUCCESS) {
                privateOSSConfigData = it.data
            }
        })
    }

    //开始上传文件
    fun uploadFile(
        ossConfigData: OSSConfigData?,
        localPath: String?,
        callBack: (String) -> Unit
    ) {
        if (localPath.isNullOrEmpty()) return
        if (ossConfigData?.bucketName == null || ossConfigData.endpoint == null) {
            showToast("配置信息有误")
            getOSSConfigData()
            return
        }
        OSSUpload.get(ossConfigData.bucketName, ossConfigData.endpoint)
            .asyncPutImage(localPath, object : OSSUpload.OnUploadListener {
                override fun onProgress(progress: Int) {
                    log("--------上传文件进度${progress}")
                }

                override fun onSuccess(request: PutObjectRequest?, result: PutObjectResult?) {
                    val fullUrl = OSSFullUrlUtils.getPublicFullUrl(
                        ossConfigData.bucketName,
                        ossConfigData.endpoint,
                        request?.objectKey ?: ""
                    )
                    log("----onSuccess--fullUrl---${fullUrl}")
                    callBack.invoke(fullUrl)
                }

                override fun onFailed(
                    request: PutObjectRequest?,
                    clientExcepion: ClientException?,
                    serviceException: ServiceException?
                ) {
                }
            })
    }

    fun hangUp(roomId: Int?, schemeId: Long?, vrType: Int?) =
        callAnswerRepository.hangUp(roomId, schemeId, vrType)

    fun hasReceived(
        roomId: Int?,
        schemeId: Long?,
        vrType: Int?
    ) = callAnswerRepository.hasReceived(roomId, schemeId, vrType)

    fun getTrtcAuthInfo(
        schemeId: Long?,
        vrType: Int?
    ) = callAnswerRepository.getTrtcAuthInfo(schemeId, vrType)


}